﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace PrismInteractionLack
{
	public partial class ViewControl : UserControl
	{

		// The "static" properties for the test sake.
		public static ViewControl SampleView1 { get; set; }
		public static ViewControl SampleView2 { get; set; }

		// The "static" constructor for the test sake.
		static ViewControl()
		{
			SampleView1 = new ViewControl();
			SampleView2 = new ViewControl();
		}


		/// <summary>
		/// The workaround is On or Off.
		/// </summary>
		public bool WorkaroundOn { get; set; }

		private ViewModel ViewModel
		{
			get
			{
				return DataContext as ViewModel;
			}
		}


		public ViewControl()
		{
			Loaded += ViewControl_Loaded;
			Unloaded += ViewControl_Unloaded;

			InitializeComponent();
		}


		void ViewControl_Loaded(object sender, RoutedEventArgs e)
		{
			if (ViewModel == null)
			{
				return;
			}

			ViewModel.PopupContent = String.Format("ViewControl's popup\n{0}", Resources["Guid"]);
		}

		/// <summary>
		/// The real workaround method.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		void ViewControl_Unloaded(object sender, RoutedEventArgs e)
		{
			if (WorkaroundOn == false)
			{
				return;
			}

			// The real workaround.
			DataContext = null;
		}




		private void MyСonfirmationRequestClick(object sender, RoutedEventArgs e)
		{
			if (ViewModel == null)
			{
				return;
			}

			ViewModel.RaiseMyСonfirmationRequest();
		}

		private void MyWeakСonfirmationRequestClick(object sender, RoutedEventArgs e)
		{
			if (ViewModel == null)
			{
				return;
			}

			ViewModel.RaiseMyWeakСonfirmationRequest();
		}

		private void MyUnloadableСonfirmationRequestClick(object sender, RoutedEventArgs e)
		{
			if (ViewModel == null)
			{
				return;
			}

			ViewModel.RaiseMyUnloadableСonfirmationRequest();
		}

	}
}
